<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
    >
      <template #timeSlot="{row}">
        <div>{{ row.timeSlot }}{{ row.timeUnit | timefilters }}</div>
      </template>
      <template slot="menuLeft">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="add()"
        >添加
        </el-button>
      </template>
      <template
        slot="menu"
        slot-scope="scope"
      >
        <el-button
          type="text"
          size="small"
          icon="el-icon-edit"
          @click="edit(scope.row)"
        >编辑
        </el-button>
        <el-button
          type="text"
          size="small"
          icon="el-icon-edit"
          @click="deleteConfig(scope.row)"
        >删除
        </el-button>
        <el-button
          type="text"
          size="small"
          icon="el-icon-edit"
          @click="relate(scope.row)"
        >关联问题
        </el-button>
      </template>
    </avue-crud>
    <addConfigDialog
      :id="id"
      v-model="configVisible"
      :action="action"
      @refreshList="(val) => getList(page, val)"
    ></addConfigDialog>
    <relateIssueDialog
      v-model="relateVisible"
      :relate-object="relateObject"
    ></relateIssueDialog>
  </basic-container>
</template>

<script>
import { getConfigList, getConfigJobById } from '@/api/core/follow';
import { tableOption } from '@/const/crud/follow/configure';
import { mapGetters } from 'vuex';
import crudMixins from '@/mixins/crud';
import addConfigDialog from './components/addConfigDialog.vue';
import relateIssueDialog from './components/relateIssueDialog.vue';
export default {
  name: 'customerManage',
  components: { addConfigDialog, relateIssueDialog },
  filters: {
    timefilters(val) {
      if (val === 'year') {
        return '年';
      } else if (val === 'month') {
        return '月';
      } else {
        return '天';
      }
    },
  },
  mixins: [crudMixins],
  data() {
    return {
      editVisible: false,
      configVisible: false,
      form: {},
      tableOption: tableOption(),
      userId: null,
      addVisible: false,
      relateVisible: false,
      action: 'add',
      id: '',
      relateObject: {},
    };
  },
  computed: {
    ...mapGetters(['permissions']),
  },
  watch: {},
  methods: {
    //添加
    add() {
      this.configVisible = true;
      this.action = 'add';
    },
    edit(row) {
      this.configVisible = true;
      this.action = 'edit';
      this.id = row.id;
    },
    relate(row) {
      console.log('关联问题', row);
      this.relateObject = row;
      this.relateVisible = true;
    },
    //删除
    deleteConfig(row) {
      this.$confirm('是否确认删除该条任务配置?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return getConfigJobById(row.id);
        })
        .then(() => {
          this.$message.success('删除成功');
          this.getList();
        });
    },
    // 列表查询
    getList(page, refresh) {
      if (refresh) {
        this.page.current = 1;
        this.page.currentPage = 1;
      }
      this.tableLoading = true;
      getConfigList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
          },
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
  },
};
</script>
